package 比屋作业.day17;
import java.util.*;
public class D17_1 {
    public static void main(String[] args) {
    Product apple=new Product("苹果",0.99);
    Product banana=new Product("香蕉",0.59);
    Product pear=new Product("梨",0.79);
    ShoppingCart cart=new ShoppingCart();
    cart.addProduct(apple);
    cart.addProduct(banana);
    cart.addProduct(pear);
    cart.printDetails();
    }
}
class Product{
    private String name;
    private double price;
    public Product(String name,double price){
        this.name=name;
        this.price=price;
    }
    public String getName(){
        return name;
    }
    public double getPrice(){
        return price;
    }

    @Override
    public String toString() {
        return "Product{name='"+ name +"',price="+price+"}";
    }
}
class ShoppingCart{
    private List<Product>products=new ArrayList<>();
    public void addProduct(Product product){
        products.add(product);
    }
    public void removeProduct(String name){
        for (int i=0;i<products.size();i++){
            if (products.get(i).getName().equals(name)){
                products.remove(i);
                break;
            }
        }
    }
    public double calculateTotal(){
        double total=0.0;
        for (Product p:products){
            total+=p.getPrice();
        }
        return total;
    }
    public void printDetails(){
        for(Product product:products){
            System.out.println(product);
        }
        double total=calculateTotal();
        System.out.println("总计："+String.format("%.2f",total));
    }
}